/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simhya.matheval;

/**
 *
 * @author Luca
 */
public class IfNode extends ExpressionNode {

   public IfNode() {
        super(3);
    }


    @Override
    String getExpressionString(boolean fullNumberRepresentation) {
        String s = "";
        s += "if(" + children[0].getExpressionString(false);
        s += "," + children[1].getExpressionString(false);
        s += "," + children[2].getExpressionString(false) + ")";
        return s;
    }

    @Override
    void computeValue() {
        children[0].computeValue();
        if (children[0].value == 1.0) {
            children[1].computeValue();
            super.value = children[1].value;
        } else {
            children[2].computeValue();
            super.value = children[2].value;
        }
    }

    @Override
    void computeValue(SymbolArray varReference) {
        children[0].computeValue(varReference);
        if (children[0].value == 1.0) {
            children[1].computeValue(varReference);
            super.value = children[1].value;
        } else {
            children[2].computeValue(varReference);
            super.value = children[2].value;
        }
    }

    @Override
    void computeValue(int evaluationCode) {
        if (random || super.evaluationCode != evaluationCode) {
            super.evaluationCode = evaluationCode;
            children[0].computeValue(evaluationCode);
            if (children[0].value == 1.0) {
                children[1].computeValue(evaluationCode);
                super.value = children[1].value;
            } else {
                children[2].computeValue(evaluationCode);
                super.value = children[2].value;
            }
        }
    }

    @Override
    void computeValue(SymbolArray varReference, int evaluationCode) {
        if (random || super.evaluationCode != evaluationCode) {
            super.evaluationCode = evaluationCode;
            children[0].computeValue(varReference,evaluationCode);
            if (children[0].value == 1.0) {
                children[1].computeValue(varReference,evaluationCode);
                super.value = children[1].value;
            } else {
                children[2].computeValue(varReference,evaluationCode);
                super.value = children[2].value;
            }
        }
    }

}
